package clientGUI.generalPanels;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import college.SysMessages;
import college.User;
import college.general;


import clientGUI.CollegeClientGUI;

import clientGUI.generalPanels.Controllers.LoginController;
import clientGUI.userPanels.UPanel;
import clientserver.Client;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class ClientLogin extends UPanel {
	private JTextField loginF;
	private JTextField passwordF;

	private int     attempts;  // how many attempts user do to log in 
    private boolean loggedin; // true if user logged in system,
   private SysMessages s;
	
    
    
    public void logout()
    {
     	controller.logout();
    }
 private LoginController controller;
	
	public String getLoggeduser() {
		return controller.getLogin();
	}



	
	
	 public void setClient( Client c)
	 {
		 controller.setClient(c);
		 s = new SysMessages(getClient());
	 }
	 
	 public Client getClient()
	 {
		 return controller.getClient();
	 }

	/**
	 * Create the panel.
	 */
	public ClientLogin( ) {
		
		super();
		attempts=0;
		loggedin=false;
		controller= new LoginController();
		
		
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblLogin = new JLabel("login:");
		GridBagConstraints gbc_lblLogin = new GridBagConstraints();
		gbc_lblLogin.insets = new Insets(0, 0, 5, 5);
		gbc_lblLogin.gridwidth = 2;
		gbc_lblLogin.gridx = 3;
		gbc_lblLogin.gridy = 2;
		add(lblLogin, gbc_lblLogin);
		
		loginF = new JTextField();
		GridBagConstraints gbc_loginF = new GridBagConstraints();
		gbc_loginF.anchor = GridBagConstraints.WEST;
		gbc_loginF.insets = new Insets(0, 0, 5, 0);
		gbc_loginF.gridx = 5;
		gbc_loginF.gridy = 2;
		add(loginF, gbc_loginF);
		loginF.setColumns(10);
		
		JLabel lblPassword = new JLabel("password:");
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 3;
		gbc_lblPassword.gridy = 3;
		add(lblPassword, gbc_lblPassword);
		
		passwordF = new JTextField();
		GridBagConstraints gbc_passwordF = new GridBagConstraints();
		gbc_passwordF.insets = new Insets(0, 0, 5, 0);
		gbc_passwordF.anchor = GridBagConstraints.WEST;
		gbc_passwordF.gridx = 5;
		gbc_passwordF.gridy = 3;
		add(passwordF, gbc_passwordF);
		passwordF.setColumns(10);
		
		JButton btnNewButton = new JButton("Log In");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
						
				
				String login=loginF.getText();   // read user login from GUI
				String enteredpassword=passwordF.getText();  // read user password from GUI
				
				
				// Controller Init : 
			
							controller.setLogin(login);
			controller.SetPassword(enteredpassword);
					 controller.docheck();
				
							
						if (controller.correctlogin()==false) myframe.alert("Incorrect login");
						
						else  // correct login  -> check password
						{
						if(controller.isloggedin()) // password  ok continue checking
						{ 
							if( controller.multilogin()) general.alert("Multiple logon denied.Log out first");
							else  // all ok  - password ok, and also user is not logged in -> continue
							{
								
							// save logged user in  father frame
							User user=controller.getlogedUser();
							myframe.setUserlogged(user);
							// set panel according to logged user
							//System.out.println(login);
							 
							myframe.SetMainUserPanel();
							}
						}
						else
						{
						
						attempts++;
						myframe.alert("Wrong password for this user");
					if(attempts==3) {myframe.alert("3 errors in password. closing"); System.exit(2); }
					}
					}  // end correct login section 
			        
						s.getmsg(login);    //if there is msg send to the user
			}  // listener end
		});
		
			GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton.anchor = GridBagConstraints.WEST;
		gbc_btnNewButton.gridx = 5;
		gbc_btnNewButton.gridy = 4;
		add(btnNewButton, gbc_btnNewButton);
		
setName("Login");
	}







}
